#import "MapSearchUtility.h"

// Shared instance of the map search utility
static MapSearchUtility* sharedMapSearchUtility = nil;

/**
 * Provides an interface in between the Mobile Assistance Subsystem and 
 * the Event Subsystem.
 * 
 * Allows for the interaction between responses and requests.
 *
 * @author      Justin Hollander
 * @version     0.5.0
 * @since       0.3.0
 * @see         SFUEventSearchEngineDelegate
 */
@implementation MapSearchUtility

/**
 * Returns the shared map search utility.
 *
 * @return  the shared map search utility
 */
+ (MapSearchUtility*)sharedMapSearchUtility {
	@synchronized(self) {
		if (sharedMapSearchUtility == nil) {
			sharedMapSearchUtility = [[MapSearchUtility alloc] init];
		}
	}
	
	return sharedMapSearchUtility;
}

/**
 * Attempts to match search parameters to a building. 
 *
 * If a match is found the corresponding building is returned.
 *
 * @param searchText  search parameter that contains building name keyword
 * @return            matching buiding or null if no match
 */
+ (Building*)searchByBuilding:(NSString*)searchText {
	if (searchText != nil) {
		return [ParsingUtilities parseBuildingFromString:searchText];
	}
	
	return nil;
}

/**
 * Keyword event search.
 *
 * @param searchText  the text used to search for events
 */
- (void)searchByEvent:(NSString*)searchText {
	EventManager* em = [EventManager sharedEventManager];
	[em setSearchDelegate:self];
	if (em == nil) {
		return;
	}
	
	if(searchText != nil) {
		[em searchLocEventsByKeywordString:searchText 
						  matchAllKeywords:NO 
							   hasLocation:YES];
	}
}

/**
 * Sets the delegate for the event manager and calls the event manager
 * to search for events. 
 *
 * This leads to the viewing of the EventSearchViewer.
 * 
 * @param navController  the main application navigation controller 
 * @param delegate       the delegate for handling event search responses
 */
- (void)searchEventsWithNavController:(UINavigationController*)navController 
					   searchDelegate:(id<SFUEventSearchResponseDelegate>)delegate {
	eventSearchDelegate = delegate;
	[EventManager searchEventsWithNavController:navController 
								 searchDelegate:self];
}

/**
 * Sets the delegate for handling event search response.
 * 
 * @param delegate  the delegate handling event search response
 */
- (void)setDelegate:(id<SFUEventSearchResponseDelegate>)delegate {
	eventSearchDelegate = delegate;
}

/**
 * Takes a category list and searches for points of interests.
 * 
 * @param categoryList  the list of categories used to search
 * @return              the points of interest found
 */
- (NSArray*)searchByPOI:(NSArray*)categoryList {	
	NSArray* POIMatchArray = [[NSArray alloc] init];
	PointOfInterestCollection* poic = [PointOfInterestCollection sharedPointOfInterestCollection];
	
	if ((categoryList != nil) && (poic != nil)) {
		for (NSString* poiCategory in categoryList) {
			[POIMatchArray arrayByAddingObjectsFromArray:
			 [poic findDataTypesWithKeywordString:poiCategory matchAllKeywords:NO]];
		}
	}
	
	return POIMatchArray;
}

/**
 * Handle a search failed event.
 *
 * @param message  the error message from the failed search
 */
- (void)searchDidFailWithError:(NSString*)message {
	if (eventSearchDelegate != nil) {
		[eventSearchDelegate eventResponseBySearchFailure:message];
	}
}

/**
 * Handle a search complete event.
 *
 * @param eventResults  the array of matched events if found;
 *                      nil otherwise.
 */
- (void)searchDidFinishWithResults:(NSArray*)eventResults {	
	if (eventSearchDelegate != nil) {
		[eventSearchDelegate eventResponseBySearchSuccess:eventResults];
	}
}

/**
 * Deallocate the map search utility.
 */
- (void)dealloc {
	[sharedMapSearchUtility release], sharedMapSearchUtility = nil;
	[super dealloc];
}

@end
